package com.example.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;

import com.example.web.filter.ServletFilter2;
import com.example.web.servlet.ComponentRegistrationServlet2;

@SpringBootApplication
/**
 * servlet第一种方式注解扫描
 * 
 * @author yzl
 *
 */
@ServletComponentScan("com.example.web")
public class BootServletFilterApplication {

	public static void main(String[] args) {
		SpringApplication.run(BootServletFilterApplication.class, args);
	}

	// 入口类注册Filter
	@Bean // 将方法中返回的对象注入到IOC容器中
	public FilterRegistrationBean filterRegiste() {
		FilterRegistrationBean reFilter = new FilterRegistrationBean();
		// 创建并注册TestFilter
		reFilter.setFilter(new ServletFilter2());
		// 拦截的路径（对所有请求拦截）
		reFilter.addUrlPatterns("/*");
		// 拦截器的名称
		reFilter.setName("组件注册");
		// 拦截器的执行顺序。数字越小越先执行
		reFilter.setOrder(1);
		return reFilter;
	}

	/**
	 * servlet第二种方式组件注册
	 * 
	 * @return
	 */
	@Bean // 一定要加，不然这个方法不会运行
	public ServletRegistrationBean getServletRegistrationBean() { // 一定要返回ServletRegistrationBean
		// 放入自己的Servlet对象实例
		ServletRegistrationBean bean = new ServletRegistrationBean(new ComponentRegistrationServlet2());
		bean.addUrlMappings("/hello2.do"); // 访问路径值
		return bean;
	}
}
